package cn.webrx;

import java.util.concurrent.TimeUnit;

public class Ticket implements Runnable {

    public static void main(String[] args) {
        Ticket tk = new Ticket(4);
        new Thread(tk, "东风路").start();
        new Thread(tk, "瑞达路").start();
        new Thread(tk, "南阳路").start();
        new Thread(tk, "科学大道").start();
        new Thread(tk, "火车站").start();
    }

    private int num = 100;

    public Ticket() {
    }

    public Ticket(int num) {
        this.num = num;
    }

    @Override
    public void run() {
        String t = Thread.currentThread().getName();
        System.out.printf("%s开启售票%n", t);
        while (true) {
            synchronized (this) {
                if (this.num < 1) {
                    break;
                } else {
                    try {
                        //System.out.printf("%s:售出一张票，剩余%d张票%n", t, --num);
                        System.out.printf("%s:售出一张票(座号:%d)，剩余%d张票%n", t, num,--num);
                        TimeUnit.SECONDS.sleep(1);
                    } catch (Exception e) {

                    }
                }
            }
        }
    }
}
